import store from "@/store/store";
import { BreadcrumbItem, MenuItem } from "@/types/Interfaces";

export function getMenus(menus = [{}]) {
  let menuArray: any = [];
  menus.map((item: any) => {
    let obj: any = {
      name: "",
      path: "",
      icon: "",
      children: [],
    };
    obj.name = item.name;
    obj["path"] = item.path;
    obj["icon"] = item.icon;
    item.menuItems.map((menu: any) => {
      let menuobj: any = {
        name: "",
        path: "",
        icon: "",
      };
      menuobj["name"] = menu.name;
      menuobj["path"] = "/layout/" + menu.path;
      menuobj["icon"] = menu.icon;
      obj["children"].push(menuobj);
    });
    menuArray.push(obj);
  });
  menuArray.unshift({
    name: "首页",
    path: "",
    icon: "Setting",
    children: [
      {
        name: "控制台",
        path: "/layout/home",
        icon: "Position",
      },
    ],
  });
  return menuArray;
}

/**
 * 获取面包屑
 * @param name
 * @returns
 */
export const getBreadcrumb = (name: string) => {
  let breadcrumbs: any = [];
  const menus: any = store.state.menuArray;
  menus.forEach((item: any) => {
    item.children.forEach((subItem: any) => {
      if (subItem.name === name) {
        let objPre: any = {};
        objPre["name"] = item.name;
        objPre["path"] = item.path;
        breadcrumbs[0] = objPre;
        let objNext: any = {};
        objNext["name"] = subItem.name;
        objNext["path"] = subItem.path;
        breadcrumbs[1] = objNext;
        return breadcrumbs;
      }
    });
  });
  return breadcrumbs;
};
